USE [DBSwindles]
GO
/******Add foregin keys   ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
				WHERE lower(constraint_name) = 'fk_tblapplicationcustomer') 
	BEGIN
	ALTER TABLE [dbo].[tblApplication] ADD CONSTRAINT FK_tblApplicationCustomer 
	FOREIGN KEY  (customerID) REFERENCES [dbo].[tblCustomer](customerID);
	PRINT 'FK_tblApplicationCustomer created'
	END
ELSE
	PRINT 'FK_tblApplicationCustomer exists'
GO

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
				WHERE lower(constraint_name) = 'fk_tblapplicationquote') 
	BEGIN
	ALTER TABLE [dbo].[tblApplication] ADD CONSTRAINT FK_tblApplicationQuote 
	FOREIGN KEY  (quoteID) REFERENCES [dbo].[tblQuote](quoteID);
	PRINT 'FK_tblApplicationQuote created'
	END
ELSE
	PRINT 'FK_tblApplicationQuote exists'
GO

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
				WHERE lower(constraint_name) = 'fk_tblapplicationuser') 
	BEGIN
	ALTER TABLE [dbo].[tblApplication] ADD CONSTRAINT FK_tblApplicationUser 
	FOREIGN KEY  (StatusUpdateUserID) REFERENCES [dbo].[tblUser](userID);
	PRINT 'FK_tblApplicationUser created'
	END
ELSE
	PRINT 'FK_tblApplicationUser exists'
GO


IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
				WHERE lower(constraint_name) = 'fk_tblquotecustomer') 
	BEGIN
	ALTER TABLE [dbo].[tblQuote] ADD CONSTRAINT FK_tblQuoteCustomer 
	FOREIGN KEY  (CustomerID) REFERENCES [dbo].[tblCustomer](customerID);
	PRINT 'FK_tblQuoteCustomer created'
	END
ELSE
	PRINT 'FK_tblQuoteCustomer exists'
GO

